package com.heatup.apt.management.controller;

import com.alibaba.fastjson.JSONObject;
import com.heatup.apt.common.util.ResultBean;
import com.heatup.apt.management.service.LoginService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

/**
 * 登录
 * <P>File name : LoginController.java </P>
 * <P>Author : fangxiaowen </P>
 * <P>Date : 2016年9月19日 </P>
 */
@Controller
@RequestMapping("/system")
public class LoginController extends BaseController {

    public static final Logger log = LoggerFactory.getLogger(LoginController.class);

    @Resource
    private LoginService loginService;

    /**
     * 跳转到后台主页面
     * LoginController.to_index()<BR>
     * <P>Author :  fangxiaowen </P>
     * <P>Date : 2016年9月20日 </P>
     *
     * @return
     */
    @RequestMapping(value = "to_index", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView to_index() {
        ModelAndView model = new ModelAndView("index");
        return model;
    }

    /**
     * 跳转到登录页面
     * LoginController.to_login()<BR>
     * <P>Author :  fangxiaowen </P>
     * <P>Date : 2016年9月20日 </P>
     *
     * @return
     */
    @RequestMapping(value = "/pre/to_login", method = {RequestMethod.POST, RequestMethod.GET})
    public ModelAndView to_login() {
        ModelAndView model = new ModelAndView("login");
        return model;
    }

    /**
     * 登录
     *
     * @param request
     * @param response
     * @param userName 用户名/手机号
     * @param userPwd  用户密码
     */
    @RequestMapping(value = "/pre/login", method = {RequestMethod.POST, RequestMethod.GET})
    public void login(HttpServletRequest request, HttpServletResponse response,
                      @RequestParam String userName, @RequestParam String userPwd,
                      StringBuffer returnUrl
                      ) {
        ResultBean resultBean = new ResultBean();
        try {
            resultBean = loginService.login(userName.trim(), userPwd.trim(), request);
            if(StringUtils.isNotBlank(returnUrl) && returnUrl.toString().contains("management/system")){
                returnUrl = new StringBuffer(request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+"/").append(returnUrl);
                Map<String, Object> dataMap = new HashMap<>();
                dataMap.put("returnUrl",returnUrl);
                resultBean.setData(dataMap);
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error("管理后台登陆出错 ---> " + e.toString() + e.getCause());
        }
        JSONObject dataJson = new JSONObject();
        dataJson.put("data", resultBean);
        responseSendMsg(response, dataJson.toString());
    }


    /**
     * 登出
     * LoginController.logout()<BR>
     * <P>Author :  fangxiaowen </P>
     * <P>Date : 2016年9月20日 </P>
     *
     * @param session
     */
    @RequestMapping(value = "/pre/logout", method = {RequestMethod.POST, RequestMethod.GET})
    @ResponseBody
    public void logout(HttpSession session) {
        try {
            session.invalidate();
        } catch (Exception e) {
            log.error("管理后台登出出错 ---> " + e.getMessage() + e.toString());
        }
    }

}
